package com.link2room.soundwave.facade.impl;

import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.DateUtil;
import com.link2room.soundwave.dto.param.inside.PmsRoomAvailInsideParamDto;
import com.link2room.soundwave.dto.result.base.PmsRmtypeAvailDto;
import com.link2room.soundwave.facade.IPmsRoomAvailHandlerFacadeService;

import java.util.List;

/**
 * Created by sss on 2017/5/18.
 */
public abstract class AbstractPmsRoomAvailHandlerFacadeServiceImpl implements IPmsRoomAvailHandlerFacadeService {

    /**
     * 同步PMS房量
     * @param pmsRoomAvailInsideParamDto
     * @throws Exception
     */
    abstract void syncPMSRoomAvail(PmsRoomAvailInsideParamDto pmsRoomAvailInsideParamDto) throws Exception;

    /**
     * 同步酒店房量
     * @param pmsRoomAvailInsideParamDto
     * @throws Exception
     */
    abstract void syncPMSHotelRoomAvail(PmsRoomAvailInsideParamDto pmsRoomAvailInsideParamDto) throws Exception;

    /**
     * 获取本地PMS库存
     * @param roomAvailInsideParamDto
     * @return
     * @throws Exception
     */
    abstract QueryResult<PmsRmtypeAvailDto> getPmsRoomAvail(PmsRoomAvailInsideParamDto roomAvailInsideParamDto) throws Exception;

    /**
     * 获取远程PMS实际库存
     * @param roomAvailInsideParamDto
     * @return
     * @throws Exception
     */
    abstract List<PmsRmtypeAvailDto> getPmsRoomAvailDirect(PmsRoomAvailInsideParamDto roomAvailInsideParamDto) throws Exception;

    /**
     * 删除房量
     * @param baseL2rParamDto
     * @throws Exception
     */
    abstract void deletePmsRoomAvail(BaseL2rParamDto baseL2rParamDto)throws Exception;


    @Override
    public void syncPMSRoomAvailHandler(PmsRoomAvailInsideParamDto pmsRoomAvailInsideParamDto) throws Exception{
        syncPMSRoomAvail(pmsRoomAvailInsideParamDto);
    }

    @Override
    public void syncPMSHotelRoomAvailHandler(PmsRoomAvailInsideParamDto pmsRoomAvailInsideParamDto) throws Exception{
        syncPMSHotelRoomAvail(pmsRoomAvailInsideParamDto);
    }

    @Override
    public QueryResult<PmsRmtypeAvailDto> getPmsRoomAvailHandler(PmsRoomAvailInsideParamDto roomAvailInsideParamDto) throws Exception {
        return this.getPmsRoomAvail(roomAvailInsideParamDto);
    }

    @Override
    public QueryResult<PmsRmtypeAvailDto> getPmsRoomAvailDirectHandler(PmsRoomAvailInsideParamDto roomAvailInsideParamDto) throws Exception {
        List<PmsRmtypeAvailDto> pmsRmtypeAvailSyncDtos = getPmsRoomAvailDirect(roomAvailInsideParamDto);
        QueryResult<PmsRmtypeAvailDto> pmsRmtypeAvailSyncDtoQueryResult=this.getPmsRoomAvailHandler(roomAvailInsideParamDto);
        if(pmsRmtypeAvailSyncDtos!=null&&pmsRmtypeAvailSyncDtos.size()>0){
            for ( PmsRmtypeAvailDto pras:pmsRmtypeAvailSyncDtoQueryResult.getResultlist()) {
                for ( PmsRmtypeAvailDto ipms2rld : pmsRmtypeAvailSyncDtos) {
                    if (pras.getRmtype().equals(ipms2rld.getRmtype())&&
                            DateUtil.getWebDateString(pras.getRsvDate()).equals(DateUtil.getWebDateString(ipms2rld.getRsvDate()))){
                        pras.setSaleNum(ipms2rld.getSaleNum());
                        pras.setAvail(ipms2rld.getAvail());
                    }
                }
            }
        }else {
            for ( PmsRmtypeAvailDto pras:pmsRmtypeAvailSyncDtoQueryResult.getResultlist()) {
                pras.setSaleNum(0);
                pras.setAvail(0);
            }
        }
        return ClassConverUtil.copyQueryResultToDtoQueryResult( pmsRmtypeAvailSyncDtoQueryResult,PmsRmtypeAvailDto.class, false);
    }

    @Override
    public void deletePmsRoomAvailHandler(BaseL2rParamDto baseL2rParamDto)throws Exception{
        deletePmsRoomAvail(baseL2rParamDto);
    }
}
